﻿// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================

using System.ServiceModel.Channels;
using System.Xml;
using System.Xml.Linq;

namespace SL.IdentityModel.Protocols.WSTrust
{
	/// <summary>
	/// inherits from BodyWriter, used to write RequestSecurityToken message body
	/// </summary>
	internal class WSTrustRequestBodyWriter : BodyWriter
	{
		private RequestSecurityToken _requestSecurityToken;
		private XNamespace namespaceUri;

		/// <summary>
		/// creates an instance of the class
		/// </summary>
		/// <param name="requestSecurityToken">RequestSecurityToken instance to be used to create the message</param>
		public WSTrustRequestBodyWriter(RequestSecurityToken requestSecurityToken, XNamespace trustNamespace)
			: base(true)
		{
			_requestSecurityToken = requestSecurityToken;

			if (trustNamespace == "http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue")
			{
				this.namespaceUri = WSTrust13Constants.NamespaceURI;
			}
			else
			{
				this.namespaceUri = "http://schemas.xmlsoap.org/ws/2005/02/trust";
			}
		}

		/// <summary>
		/// Writes contents of message body
		/// </summary>
		/// <param name="writer">XmlDictionaryWriter to write message to</param>
		protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
		{
			WSTrust13SerializationHelper serializationHelper = new WSTrust13SerializationHelper(this.namespaceUri);

			serializationHelper.WriteXml(_requestSecurityToken, writer);
		}
	}
}
